package com.midian.controller.organization;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.midian.common.filter.annotation.Validate;
import com.midian.common.system.RetManager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.CheckUtils;
import com.midian.common.utils.PicUtils;
import com.midian.controller.device.DeviceController;
import com.midian.model.device.Device;
import com.midian.model.organization.OrganizationJoin;
import com.midian.model.pic.Pic;
import com.midian.service.device.IDeviceService;
import com.midian.service.organization.IOrganizationJoinService;
import com.sun.xml.internal.bind.v2.TODO;

@Controller("organizationController")
public class OrganizationController {
	/** 日志 */
	private static Logger LOG = Logger.getLogger(OrganizationController.class);
    
	
	/** 设备号表业务 */
	private IOrganizationJoinService organizationJoinService;
	
	PicUtils picUtils;

	@Resource(name = "picUtils")
	public void setPicUtils(PicUtils picUtils) {
		this.picUtils = picUtils;
	}

	@Resource(name = "organizationJoinService")
	public void setOrganizationJoinService(IOrganizationJoinService organizationJoinService) {
		this.organizationJoinService = organizationJoinService;
	}
	
	
	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/organization_join_apply", method = RequestMethod.POST)
	public @ResponseBody
	Object organizationJoinApply(String client_key, String name,String contact_person,String contact_num,String contact_address,@RequestParam(required=false)String website,@RequestParam(required=false)String remark,
			@RequestParam(value="business_license",required=false) MultipartFile business_license) throws Exception {
		LOG.debug("======【organizationController】进入organization_join_apply========");
		JSONObject json = new JSONObject();
		try {
			JSONObject contentJson = new JSONObject();
	        //TODO ===leeib=== 需要处理图片
			OrganizationJoin organizationJoin = new OrganizationJoin();
			organizationJoin.setName(name);
			organizationJoin.setContactPerson(contact_person);
			organizationJoin.setContactNum(contact_num);
			organizationJoin.setContactAddress(contact_address);
			organizationJoin.setWebsite(website);
			organizationJoin.setRemark(remark);
			//因缺少状态字段 初始化加盟商审核状态为0未审核
			organizationJoin.setDelFlag(0);
			if (business_license != null) {
				LOG.debug("======【organizationController】收到营业执照对象============="+business_license);
				Pic pic = picUtils.uploadPic(business_license); // 头像
				LOG.debug("======【organizationController】上传成功=========");
				if (pic != null) {
					organizationJoin.setBusinessLicense(pic.getId());
					LOG.debug("======【organizationController】设置营业执照为上传图片的id==========");
				}
				LOG.debug("======【organizationController】图像处理完成==================");
			}
			LOG.debug("======【organizationController】数据库中添加加盟商数据==================");
			OrganizationJoin targetOrganizationJoin = organizationJoinService.add(organizationJoin);
            if(StringUtils.isBlank(targetOrganizationJoin.getId())){
            	RetManager.getInstance().setRetError(json, RetManager.common_submit_fail);
            }			
			RetManager.getInstance().setRetSuccess(json, RetManager.common_submit_success);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		return json;
	}

}
